#!/bin/bash

# K8s MCP 服务器基本功能测试脚本

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
BINARY="$PROJECT_ROOT/bin/k8s-mcp-server"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 测试函数
test_binary_exists() {
    log_info "测试二进制文件是否存在..."
    if [ -f "$BINARY" ]; then
        log_success "二进制文件存在: $BINARY"
        return 0
    else
        log_error "二进制文件不存在: $BINARY"
        return 1
    fi
}

test_binary_executable() {
    log_info "测试二进制文件是否可执行..."
    if [ -x "$BINARY" ]; then
        log_success "二进制文件可执行"
        return 0
    else
        log_error "二进制文件不可执行"
        return 1
    fi
}

test_dependencies() {
    log_info "测试依赖工具..."
    
    # 测试Go
    if command -v go &> /dev/null; then
        log_success "Go 已安装: $(go version | cut -d' ' -f3)"
    else
        log_warning "Go 未安装"
    fi
    
    # 测试kubectl
    if command -v kubectl &> /dev/null; then
        log_success "kubectl 已安装"
    else
        log_warning "kubectl 未安装"
    fi
}

test_help_output() {
    log_info "测试程序帮助输出..."
    
    # 设置测试模式环境变量
    export SERVER_MODE="stdio"
    
    # 由于MCP服务器可能会等待输入，我们只测试它是否能启动
    timeout 5s "$BINARY" --help 2>/dev/null || true
    
    if [ $? -eq 0 ] || [ $? -eq 124 ]; then
        log_success "程序可以启动（退出码正常或超时）"
        return 0
    else
        log_warning "程序启动可能有问题"
        return 1
    fi
}

test_config_file() {
    log_info "测试配置文件..."
    
    local config_example="$PROJECT_ROOT/config/config.env.example"
    if [ -f "$config_example" ]; then
        log_success "配置文件示例存在: $config_example"
        return 0
    else
        log_error "配置文件示例不存在"
        return 1
    fi
}

test_project_structure() {
    log_info "测试项目结构..."
    
    local required_dirs=("src" "config" "bin" "docs" "service")
    local missing_dirs=()
    
    for dir in "${required_dirs[@]}"; do
        if [ ! -d "$PROJECT_ROOT/$dir" ]; then
            missing_dirs+=("$dir")
        fi
    done
    
    if [ ${#missing_dirs[@]} -eq 0 ]; then
        log_success "项目结构完整"
        return 0
    else
        log_error "缺少目录: ${missing_dirs[*]}"
        return 1
    fi
}

test_source_files() {
    log_info "测试源代码文件..."
    
    local required_files=("main.go" "k8s_mcp.go" "k8s_handlers.go" "k8s_register.go" "k8s_utils.go" "go.mod")
    local missing_files=()
    
    for file in "${required_files[@]}"; do
        if [ ! -f "$PROJECT_ROOT/src/$file" ]; then
            missing_files+=("$file")
        fi
    done
    
    if [ ${#missing_files[@]} -eq 0 ]; then
        log_success "源代码文件完整"
        return 0
    else
        log_error "缺少源文件: ${missing_files[*]}"
        return 1
    fi
}

# 主测试函数
run_tests() {
    local passed=0
    local total=0
    
    echo "=========================================="
    echo "         K8s MCP 服务器基本测试"
    echo "=========================================="
    
    # 运行各项测试
    local tests=(
        "test_project_structure"
        "test_source_files"
        "test_config_file"
        "test_binary_exists"
        "test_binary_executable"
        "test_dependencies"
        "test_help_output"
    )
    
    for test in "${tests[@]}"; do
        echo
        total=$((total + 1))
        if $test; then
            passed=$((passed + 1))
        fi
    done
    
    echo
    echo "=========================================="
    echo "             测试结果汇总"
    echo "=========================================="
    echo "通过测试: $passed/$total"
    
    if [ $passed -eq $total ]; then
        log_success "所有测试通过！K8s MCP 服务器基本功能正常"
        return 0
    else
        log_warning "部分测试未通过，请检查相关问题"
        return 1
    fi
}

# 显示项目信息
show_project_info() {
    echo "=========================================="
    echo "           项目信息"
    echo "=========================================="
    echo "项目名称: Kubernetes MCP 服务器"
    echo "项目路径: $PROJECT_ROOT"
    echo "二进制文件: $BINARY"
    echo "配置目录: $PROJECT_ROOT/config"
    echo "文档目录: $PROJECT_ROOT/docs"
    echo
    
    if [ -f "$BINARY" ]; then
        echo "二进制文件信息:"
        ls -lh "$BINARY"
    fi
    
    echo
}

# 主函数
main() {
    case "${1:-}" in
        --info)
            show_project_info
            ;;
        --help)
            echo "K8s MCP 服务器测试脚本"
            echo
            echo "用法: $0 [选项]"
            echo
            echo "选项:"
            echo "  --info    显示项目信息"
            echo "  --help    显示此帮助"
            echo "  (无参数)  运行基本测试"
            ;;
        "")
            show_project_info
            run_tests
            ;;
        *)
            log_error "未知选项: $1"
            echo "使用 --help 查看帮助"
            exit 1
            ;;
    esac
}

# 运行主函数
main "$@" 